/**
 * @class cceaf.control.MultipleGrid
 * @extends Ext.panel.Panel
 * 
 * 两表格选择器	
 * 
 * @author 攻心小虫
 * 
 * 例子：
 * {
 * 	xtype:'multiplegrid',
 *	model:'XXXX',			//数据结构
 * 	columns:[],				//表头
 * 	toQueryAction:'',		//右边表格请求
 * 	formQueryAction:''		//左边表格请求
 * }
 */
Ext.define('cceaf.control.MultipleGrid', {
    extend: 'Ext.panel.Panel',
    xtype: 'multiplegrid',
    requires: [
        'Ext.grid.*'
    ], 
    //必须参数
    model:null,				
    formQueryAction:'',		
    toQueryAction:'',		
    columns :null,			
    //--
    layout: {
        type: 'hbox',
        align: 'stretch',
        padding: 5
    },
    tbar:['-',{
    	text:'向右',
    	handler:function(){
    		var grid = this.up().up().items.items[0];
    		var selModel = grid.getSelectionModel();
	    	var selectedCount = selModel.getCount();
	    	if(selectedCount==0){
	    		Ext.Msg.alert('错误','必须在左边表格中选择数据');
	    		return;
	    	}else{
	    		if(this.up().up().vaild(selModel.getSelection())){
		    		var grid_ = this.up().up().items.items[1];
		    		grid_.getStore().add(selModel.getSelection());
		    		grid.getStore().remove(selModel.getSelection());
	    		}
	    	}
    	}
    },'-',{text:'全部向右',handler:function(){
    	var grid = this.up().up().items.items[0];
    	var grid_ = this.up().up().items.items[1];
    	if(this.up().up().vaild(grid.getStore())){
			grid.getStore().each(function(record) {
			   grid_.getStore().add(record);
			});
			grid.getStore().removeAll();
    	}
    }},'-',{text:'重置',handler:function(){
    	var grid = this.up().up().items.items[1];
    	var grid_ = this.up().up().items.items[0];
    	grid.getStore().reload();
    	grid_.getStore().reload();
    }},'-','->','-',{text:'全部向左',handler:function(){
    	var grid = this.up().up().items.items[1];
    	var grid_ = this.up().up().items.items[0];
		if(this.up().up().vaild(grid.getStore())){
			grid.getStore().each(function(record) {
				grid_.getStore().add(record);
			});
			grid.getStore().removeAll();
		}
    	
    }},'-',{
    	text:'向左',
    	handler:function(){
    		var grid = this.up().up().items.items[1];
    		var selModel = grid.getSelectionModel();
	    	var selectedCount = selModel.getCount();
	    	if(selectedCount==0){
	    		Ext.Msg.alert('错误','必须在左边表格中选择数据');
	    		return;
	    	}else{
	    		if(this.up().up().vaild(selModel.getSelection())){
		    		var grid_ = this.up().up().items.items[0];
		    		grid_.getStore().add(selModel.getSelection());
		    		grid.getStore().remove(selModel.getSelection());
	    		}
	    	}
    	}
    },'-'],
    initComponent: function(){
    	var config = this;
        this.items = [{
            flex: 1,
            xtype: 'grid',
            multiSelect: true,
            viewConfig: {
                plugins: {
                    ptype: 'gridviewdragdrop'
                },
                listeners: {
                    drop: function(node, data, dropRec, dropPosition) {
                    	if(!config.vaild(data.records)){
                    		var grid = this.up();
                    		var grid_ = grid.up().items.items[1];
            	    		grid_.getStore().add(data.records);
            	    		grid.getStore().remove(data.records);
                    	}
                    }
                }
            },
            listeners:{
            	itemdblclick:function(g,r){
            		var grid = g.up();
            		var grid_ = grid.up().items.items[1];
    	    		grid_.getStore().add(r);
    	    		grid.getStore().remove(r);
            	}
            },
            store:new Ext.data.Store({
    	    	proxy: {
    	    		type: 'ajax',
    	    		url:  config.formQueryAction,
    	    		actionMethods: {  
    		            read: 'POST'  
    		        },  
    	    		reader: {
    					type: 'json',
    					root: 'data',
    					totalProperty: 'totalCount',
    					messageProperty: 'message'
    				}
    			},
    			
    	        listeners: {
    	        	'load': function(ds){
    	        		var jr = ds.getProxy().getReader();
    	        		if(!jr.jsonData) {
    	        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
    	        			return;
    	        		}
    	        		
    	        		var message = jr.jsonData.message;
    	        		var url = jr.jsonData.url;
    	        		cceaf.util.ExtHelper.showInfoDialog(message,url);
    	        	},
    	        	'loadexception': function(ds){
    	        		var jr = ds.getProxy().getReader();
    	        		if(jr.jsonData){
    		        		var message = jr.jsonData.message;
    		        		var url = jr.jsonData.url;
    		        		cceaf.util.ExtHelper.showErrorDialog(message,url);
    	        		}
    	        		else{
    	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
    	        		}
    	        	}
    	        	
    	        },
    	        autoDestroy: true,  
    	        autoLoad:true,
    	    	model: config.model
    	    }),
            columns: this.columns,
            stripeRows: true
        }, {
            flex: 1,
            xtype: 'grid',
            listeners:{
            	itemdblclick:function(g,r){
            		var grid = g.up();
            		var grid_ = grid.up().items.items[0];
    	    		grid_.getStore().add(r);
    	    		grid.getStore().remove(r);
            	}
            },
            viewConfig: {
                plugins: {
                    ptype: 'gridviewdragdrop'
                },
                listeners: {
                    drop: function(node, data, dropRec, dropPosition) {
                    	if(!obj.vaild(data.records)){
                    		var grid = this.up();
                    		var grid_ = grid.up().items.items[0];
            	    		grid_.getStore().add(data.records);
            	    		grid.getStore().remove(data.records);
                    	}
                    }
                }
            },
            store: new Ext.data.Store({
    	    	proxy: {
    	    		type: 'ajax',
    	    		url:  config.toQueryAction,
    	    		actionMethods: {  
    		            read: 'POST'  
    		        },  
    	    		reader: {
    					type: 'json',
    					root: 'data',
    					totalProperty: 'totalCount',
    					messageProperty: 'message'
    				}
    			},
    			
    	        listeners: {
    	        	'load': function(ds){
    	        		var jr = ds.getProxy().getReader();
    	        		if(!jr.jsonData) {
    	        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
    	        			return;
    	        		}
    	        		
    	        		var message = jr.jsonData.message;
    	        		var url = jr.jsonData.url;
    	        		cceaf.util.ExtHelper.showInfoDialog(message,url);
    	        	},
    	        	'loadexception': function(ds){
    	        		var jr = ds.getProxy().getReader();
    	        		if(jr.jsonData){
    		        		var message = jr.jsonData.message;
    		        		var url = jr.jsonData.url;
    		        		cceaf.util.ExtHelper.showErrorDialog(message,url);
    	        		}
    	        		else{
    	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
    	        		}
    	        	}
    	        	
    	        },
    	        autoDestroy: true,  
    	        autoLoad:true,
    	    	model: config.model
    	    }),
            columns: this.columns,
            stripeRows: true
        }];
        this.callParent();
    },
    vaild:function(){
    	
    }
});